package icu.luoxi.service;

import icu.luoxi.utils.EmailUtil;
import icu.luoxi.utils.GetOnePhotoUtil;
import icu.luoxi.utils.GetOneTextUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * @author Cao Jiwen
 * @version V1.0
 */
@Component
public class SendEmailService {

    @Value("${email.username}")
    private String username;

    @Value("${email.password}")
    private String password;

    @Value("${email.toEmail}")
    private String[] toEmails;

    @Scheduled(cron = "0 14 13 * * ?")
    public void send() throws MessagingException, UnsupportedEncodingException {
        String text = GetOneTextUtil.getOne();
        String photoUrl = GetOnePhotoUtil.getOne();
        for (String toEmail : toEmails) {
            EmailUtil.sendEmail(username,password,toEmail,"来自Luoxi的彩虹屁",getMailContent(text,photoUrl));
        }
    }

    private String getMailContent(String text,String photoUrl) {
        String body = "<div id=\"contentDiv\" onmouseover=\"getTop().stopPropagation(event);\"\n" +
                "     onclick=\"getTop().preSwapLink(event, 'html', 'ZC0020_rZDNvnqMyVcuGGMAPRMXwcb');\"\n" +
                "     style=\"position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;\"\n" +
                "     class=\"body\">\n" +
                "    <div id=\"qm_con_body\">\n" +
                "        <div id=\"mailContentContainer\" class=\"qmbox qm_con_body_content qqmail_webmail_only\" style=\"opacity: 1;\">\n" +
                "            <div style=\"background: white;\n" +
                "      width: 95%;\n" +
                "      max-width: 800px;\n" +
                "      margin: auto auto;\n" +
                "      border-radius: 5px;\n" +
                "      border:orange 1px solid;\n" +
                "      overflow: hidden;\n" +
                "      -webkit-box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.12);\n" +
                "      box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.18);\">\n" +
                "                <header style=\"overflow: hidden;\">\n" +
                "                    <img style=\"width:100%;z-index: 666;\"\n" +
                "                         src=\""+photoUrl+"\">\n" +
                "                </header>\n" +
                "                <div style=\"padding: 5px 20px;\">\n" +
                "                    <p style=\"position: relative;\n" +
                "        color: white;\n" +
                "        float: left;\n" +
                "        z-index: 999;\n" +
                "        background: orange;\n" +
                "        padding: 5px 30px;\n" +
                "        margin: -25px auto 0 ;\n" +
                "        box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.30)\">Dear 李女士</p>\n" +
                "                    <br>\n" +
                "                    <h3 class=\"i18n\" data-iname=\"mail.title\">您有一封来自<span style=\"text-decoration: none;color: orange \">洛希极限</span>的信函</h3>\n" +
                "                    <p style=\"border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px\">\n" +
                "                        "+text+"\n" +
                "                    </p>\n" +
                "                    <p style=\"font-size: 14px;\">\n" +
                "                        <span style=\"text-decoration: none;color: orange\" rel=\"noopener\">洛希极限</span>&nbsp;双手呈上~\n" +
                "                    </p>\n" +
                "                    <!--<div style=\"text-align: center;\">-->\n" +
                "                    <!--    <img src=\"https://takagi-api.com/file/1/halo-theme-sakura/1.3.4/source/images/other/hr.png\"-->\n" +
                "                    <!--         style=\"width:100%;margin:5px auto 5px auto; display: block;\">-->\n" +
                "                    <!--</div>-->\n" +
                "                    <p style=\"font-size: 12px;text-align: center;color: #999;\">\n" +
                "                        © 2024 洛希极限\n" +
                "                    </p>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <style type=\"text/css\">.qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {\n" +
                "                display: none !important;\n" +
                "            }</style>\n" +
                "        </div>\n" +
                "    </div><!-- -->\n" +
                "    <style>#mailContentContainer .txt {\n" +
                "        height: auto;\n" +
                "    }</style>\n" +
                "</div>";
        int number = getRandomNumber();
        if (number == 2){
            body = "<div style=\"width: 100%; max-width: 600px; height: auto; margin: 0; padding: 0; background-image: url('https://ubistatic-a.akamaihd.net/0175/CRM/EMAIL/BATCH/VH2/2024/WWCRM6350_HD_Launch/Background.png'); background-repeat: no-repeat; background-position: top center; background-size: 100%; background-color: #edeff0;\"> \n" +
                    "      \n" +
                    "        <div style=\"display: block; width: 100%; max-width: 600px; height: auto; margin: 0; padding: 5px 0 0px 0; text-align: center; font-size:0px;\">\n" +
                    "          <img src=\"https://ubistatic-a.akamaihd.net/0175/CRM/EMAIL/BATCH/VH2/2024/WWCRM6350_HD_Launch/Block_4/Block_4_Separator_Top.png\" alt=\"Separator\" style=\"display: inline-block; width: 100%; height: auto; max-width: 600px; border: none;\">\n" +
                    "        </div>\n" +
                    "        \n" +
                    "        <div class=\"paddingWrapper\" style=\"width: 100%; max-width: 600px; height: auto; padding: 0;\">\n" +
                    "          <div style=\"width: 100%; height: auto; margin: 0; padding: 0; background-image: url(''); background-repeat: no-repeat; background-position: top center; background-size: 100%; background-color: #d3ccc3;\">\n" +
                    "            <div class=\"columnContainer\" style=\"display: block; width: 100%; max-width: 600px; height: auto; padding: 20px 0 10px 0; text-align: center;\">   \n" +
                    "            \n" +
                    "            <h2 style=\"font-family: Palatino Linotype,Book Antiqua,Palatino,serif; font-size: 22px; font-weight: bold; line-height: 1.3; color: #000000; text-align: center; margin: 0 25px 0 25px; padding: 0 10px 20px 10px;\">\n" +
                    "              每日邮件\n" +
                    "            </h2>\n" +
                    "            \n" +
                    "            <img src=\""+photoUrl+"\" alt=\"MITTC__IMAGE\" style=\"display: block; width: 100%; height: auto; max-width: 600px; border: none; margin: 0 auto;\">\n" +
                    "            \n" +
                    "            <p style=\"font-family: Palatino Linotype,Book Antiqua,Palatino,serif; font-size: 16px; font-weight: normal; line-height: 1.5; color: #000000; text-align: center; margin: 0px 25px 10px 25px; padding: 0 30px 0 30px;\">\n" +
                    "              "+text+"\n" +
                    "            </p>\n" +
                    "          </div>\n" +
                    "        </div>\n" +
                    "      </div>\n" +
                    "  \n" +
                    "      <div style=\"display: block; width: 100%; max-width: 600px; height: auto; margin: 0; padding: 0px 0 40px 0; text-align: center; font-size:0px;\">\n" +
                    "        <img src=\"https://ubistatic-a.akamaihd.net/0175/CRM/EMAIL/BATCH/VH2/2024/WWCRM6350_HD_Launch/Block_4/Block_4_Separator_Bottom.png\" alt=\"Separator\" style=\"display: inline-block; width: 100%; height: auto; max-width: 600px; border: none;\">\n" +
                    "      </div>\n" +
                    "    </div>";
        }
        return body;
    }

    public static int getRandomNumber() {
        Random random = new Random();
        return random.nextInt(2) + 1;
    }
}
